package com.cndw.kungfu.model.activity.holiday;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.HolidayCommSerialize;
/**
 * 秦淮诗会数据
 * 
 * @author Everest
 *
 */
public class PoetryData implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 668596461319257404L;
	
	private Map<Long, Integer> scoreMap = new ConcurrentHashMap<Long, Integer>();
	
	private static List<Object[]> ranking = new ArrayList<Object[]>();
	/**
	 * 最后排行时间
	 */
	private static int lrt = 0;
	
	private int state = 0;//0 未开始  1 已开始 2 已发称号
	
	private static int endTime = TimeProcesser.getUnixTimeSilent("2012-11-12 23:59:59"); 
	
	private static String[] npcNames = {"", "李白", "杜甫", "李贺"};
	
	public static PoetryData getInstance() {
		return HolidayCommSerialize.getInstance().getPoetryData();
	}
	

	public void init() {
		if (state == 0 && scoreMap != null) {
			scoreMap.put(1L, 50000);
			scoreMap.put(2L, 30000);
			scoreMap.put(3L, 5000);
			state = 1;
		}
	}
	
	public void alterScore(long playerId, int score) {
		if (scoreMap.containsKey(playerId)) {
			scoreMap.put(playerId, scoreMap.get(playerId) + score);
		} else {
			scoreMap.put(playerId, score);
		}
	}
	
	public int getScore(long playerId) {
		if (scoreMap.containsKey(playerId)) {
			return scoreMap.get(playerId);
		}
		return 0;
	}
	
	public void rnak() {
		if (ranking == null) {
			ranking = new ArrayList<Object[]>();
		} else {
			ranking.clear();
		}
		
		if (scoreMap.isEmpty()) {
			return;
		}
		
		List<Entry<Long, Integer>> list = getRankList(scoreMap);
		int orderId = 0;
		for (Entry<Long, Integer> e : list) {
			Player player = Context.getPlayerServer().getByPlayerId(e.getKey());
			if (player == null && !isNpc(e.getKey())) {
				continue;
			}
			orderId ++;
			Object[] obj = new Object[5];
			obj[0] = orderId;
			obj[1] = e.getKey();
			obj[3] = e.getValue();
			if (isNpc(e.getKey())) {
				obj[2] = npcNames[e.getKey().intValue()];
				obj[4] = e.getKey() + 3;
			} else {
				obj[2] = player.getPlayerName();
				obj[4] =  player.getVocationId();
			}
			ranking.add(obj);
			if (orderId >= 10) {
				break;
			}
		}
		lrt = TimeProcesser.getUnixTime();
		if (chkEnd() && state == 1) {
			int titleId = 0;
			long playerId = 0;
			for (int i = 0; i < 10; i++) {
				if (i >= ranking.size()) {//排行榜不足10人
					break;
				}
				playerId = (Long) ranking.get(i)[1];
				if (isNpc(playerId)) {
					continue;
				}
				titleId = i < 3 ? 80 + i : 83;
				Context.getGeneralServer().addTitle(playerId, titleId);
			} 
			state = 2;
			LogUtil.getMain().debug("poetry send title ");
		}
	}
	
	public List<Object[]> getRanking() {
		if (TimeProcesser.getUnixTime() - lrt > 300) {
			synchronized (this) {
				if (TimeProcesser.getUnixTime() - lrt > 300) {
					rnak();
				}
			}
		}
		return ranking;
	}
	
	public List<Entry<Long, Integer>> getRankList(Map<Long, Integer> data) {
		List<Entry<Long, Integer>> list = new ArrayList<Map.Entry<Long,Integer>>(data.entrySet());
		Collections.sort(list, new Comparator<Entry<Long, Integer>>() {
			@Override
			public int compare(Entry<Long, Integer> o1, Entry<Long, Integer> o2) {
				return o2.getValue() - o1.getValue();
			}
		});
		return list;
	}
	
	public boolean isNpc(long id) {
		return id == 1 || id == 2 || id == 3;
	}
	
	public boolean chkEnd () {
		return TimeProcesser.getUnixTime() >= endTime;
	}
	
	public void clear () {
		if (scoreMap != null && ranking != null) {
			ranking.clear();
			scoreMap.clear();
			scoreMap.put(1L, 50000);
			scoreMap.put(2L, 30000);
			scoreMap.put(3L, 5000);
		}
	}
	
	public void commitSync() {
		HolidayCommSerialize.getInstance().commitSync();
	}

}
